package com.zyw.service.repertory.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.RepertoryMaterial;
import com.zyw.entity.RepertoryMaterialLog;
import com.zyw.from.repertoryFrom;
import com.zyw.mapper.repertory.RepertoryMaterialMapper;
import com.zyw.service.repertory.RepertoryMaterialLogService;
import com.zyw.service.repertory.RepertoryMaterialService;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zyw.constant.ManagermentSystemConstant.MATERIAL_REPERTORY_NUM_MAP;

@Service
@Slf4j
public class RepertoryMaterialServiceImpl implements RepertoryMaterialService {

    @Resource
    public RepertoryMaterialMapper repertoryMaterialMapper;

    @Autowired
    public RepertoryMaterialLogService repertoryMaterialLogService;

    @Override
    public List<RepertoryMaterial> selectParent() {

        Example example = new Example(RepertoryMaterial.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("typeId",0);
        List<RepertoryMaterial> repertoryMaterials = repertoryMaterialMapper.selectByExample(example);


        return repertoryMaterials;
    }

    @Override
    public LayUITableVO<List<RepertoryMaterial>> findPage(Map<String, Object> map) {


        repertoryFrom from = (repertoryFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(RepertoryMaterial.class);
        Example.Criteria c = example.createCriteria();
        c.andNotEqualTo("typeId",0);

        if (! "".equals(from.getMaterialName()) && null!=from.getMaterialName()){
            c.andLike("materialName","%"+from.getMaterialName()+"%");
        }

        if ( null!=from.getMaterialId()){
            c.andLike("typeId", String.valueOf(from.getMaterialId()));
        }
        List<RepertoryMaterial> list = repertoryMaterialMapper.selectByExample(example);
        for (RepertoryMaterial repertoryMaterial : list) {
            if (repertoryMaterial.getTypeId() == 3001){
                repertoryMaterial.setType("面料");
            }else {
                repertoryMaterial.setType("辅料");
            }
        }

        PageInfo<RepertoryMaterial> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<RepertoryMaterial>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public RepertoryMaterial selectById(String id) {
        return repertoryMaterialMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(RepertoryMaterial repertoryMaterial) {
        repertoryMaterialMapper.updateByPrimaryKeySelective(repertoryMaterial);
    }

    @Override
    @Transactional
    public void save(RepertoryMaterial repertoryMaterial) {
        repertoryMaterialMapper.insertSelective(repertoryMaterial);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        repertoryMaterialMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String,List<RepertoryMaterial>> findProcurementOrder() {

        Map<String,List<RepertoryMaterial>> map = new HashMap<>();

        Example example = new Example(RepertoryMaterial.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("typeId",3001);
        List<RepertoryMaterial> repertoryMaterials = repertoryMaterialMapper.selectByExample(example);
        map.put("lining",repertoryMaterials);

        Example example1 = new Example(RepertoryMaterial.class);
        Example.Criteria c1 = example1.createCriteria();
        c1.andEqualTo("typeId",3002);
        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialMapper.selectByExample(example1);
        map.put("ingredients",repertoryMaterialList);

        return map;
    }

    @Override
    public List<RepertoryMaterial> selectChild() {

        Example example = new Example(RepertoryMaterial.class);
        Example.Criteria c = example.createCriteria();
        c.andNotEqualTo("typeId",0);
        List<RepertoryMaterial> repertoryMaterialList = repertoryMaterialMapper.selectByExample(example);


        return repertoryMaterialList;
    }

    @Override
    public void update(Integer id, Integer repertoryNum,String orderId) {

        RepertoryMaterial repertoryMaterial = repertoryMaterialMapper.selectByPrimaryKey(id);
        Integer old = repertoryMaterial.getRepertoryNum();
        repertoryMaterial.setRepertoryNum(old-repertoryNum);

        repertoryMaterialMapper.updateByPrimaryKeySelective(repertoryMaterial);

        RepertoryMaterialLog repertoryMaterialLog = new RepertoryMaterialLog();
        repertoryMaterialLog.setMaterialId(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        repertoryMaterialLog.setDate(simpleDateFormat.format(new Date()));
        //TODO 登录人名称
        repertoryMaterialLog.setOperator("小绿");
        repertoryMaterialLog.setOperatorType("订单生产:"+orderId);
        repertoryMaterialLog.setBeforeRevision(old);
        repertoryMaterialLog.setChangeNum(0 - repertoryNum);

        repertoryMaterialLogService.save(repertoryMaterialLog);
    }
}
